package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.util.ValidationUtil;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class UserController {

    //形参前增加注解@Valid或@Validated
    @RequestMapping("register")
    public BaseRespVo register(@Valid User user, BindingResult bindingResult) {
        /*String username = user.getUsername();
        if (username == null || username.length() < 6) {
            return BaseRespVo.fail();
        }
        if (user.getPassword() == null || user.getPassword().length() < 6) {
            return BaseRespVo.fail();
        }*/
        if (bindingResult.hasFieldErrors()) {
            return ValidationUtil.valid(bindingResult);
        }
        return BaseRespVo.ok();
    }
    //形参前增加注解@Valid或@Validated
    @RequestMapping("register2")
    public BaseRespVo register2(@Valid User user, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid2(bindingResult);
        return BaseRespVo.ok();
    }
}
